package com.dongnam.quanly.common.controller;

import java.util.List;

import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dongnam.quanly.common.dao.DonHangDao;
import com.dongnam.quanly.common.exceptions.DatabaseException;
import com.dongnam.quanly.common.model.DonHang;
import com.dongnam.quanly.common.model.VatTuHangHoa;
import com.dongnam.quanly.common.model.enumtype.LoaiDonHang;
import com.dongnam.quanly.common.model.enumtype.TrangThaiDonHang;
import com.dongnam.quanly.common.persistence.HibernateUtil;

public class DonHangController extends GenericController<DonHang, String> {

	private static final Logger logger = LoggerFactory.getLogger(DonHangController.class);

	public DonHangController() {
		super(new DonHangDao());
		// TODO Auto-generated constructor stub
	}

	@Override
	public DonHangDao getDao() {
		// TODO Auto-generated method stub
		return (DonHangDao) dao;
	}

	public List<VatTuHangHoa> getListMaVthhByDonHang(String donhang) {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();
		try {
			List<VatTuHangHoa> list = getDao().getListMaVthhByDonHang(donhang);
			if (!isTransacted) {
				session.getTransaction().commit();
			}
			return list;
		} catch (Throwable e) {
			rollbackConditionally(isTransacted, session);
			logger.debug("exception", e);
			throw new DatabaseException(e);
		}

	}

	public List<DonHang> getListDonHang(String doitac, TrangThaiDonHang trangthai) {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();
		try {
			List<DonHang> list = getDao().getListDonHang(doitac, trangthai);
			if (!isTransacted) {
				session.getTransaction().commit();
			}
			return list;
		} catch (Throwable e) {
			rollbackConditionally(isTransacted, session);
			logger.debug("exception", e);
			throw new DatabaseException(e);
		}

	}

	public List<DonHang> getListDonHang(TrangThaiDonHang trangthai) {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();
		try {
			List<DonHang> list = getDao().getListDonHang(trangthai);
			if (!isTransacted) {
				session.getTransaction().commit();
			}
			return list;
		} catch (Throwable e) {
			rollbackConditionally(isTransacted, session);
			logger.debug("exception", e);
			throw new DatabaseException(e);
		}

	}

	public List<DonHang> getListDonHang(TrangThaiDonHang[] trangthai, LoaiDonHang loaiDonHang) {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();
		try {
			List<DonHang> list = getDao().getListDonHang(trangthai, loaiDonHang);
			if (!isTransacted) {
				session.getTransaction().commit();
			}
			return list;
		} catch (Throwable e) {
			rollbackConditionally(isTransacted, session);
			logger.debug("exception", e);
			throw new DatabaseException(e);
		}

	}

}
